import { Application } from 'egg'
import { EntityController } from './core'

const routeEntity = (app: Application, routeName: string, entityController: EntityController, options = null) => {
  // const { list, find, create, update, del, patch, query } = options
  if (options) app.logger.info('routeEntity options', options)
  const { router, config } = app
  const { entityRouterPath } = config.settings
  router.get(`${entityRouterPath}${routeName}`, entityController.list)
  // router.get(`${entityRouterPath}${routeName}/all`, controllerObj.all)
  router.get(`${entityRouterPath}${routeName}/count`, entityController.count)
  router.get(`${entityRouterPath}${routeName}/build`, entityController.build)
  router.get(`${entityRouterPath}${routeName}/:id`, entityController.find)
  router.post(`${entityRouterPath}${routeName}`, entityController.create)
  router.put(`${entityRouterPath}${routeName}/:id`, entityController.update)
  router.del(`${entityRouterPath}${routeName}/:id?`, entityController.del)
  router.patch(`${entityRouterPath}${routeName}/:id`, entityController.patch)
  router.post(`${entityRouterPath}${routeName}/query`, entityController.query)
}

const routeEntities = (app: Application) => {
  const ce = app.controller.entity
  routeEntity(app, 'cities', ce.city)
}

export default (app: Application) => {
  const { controller, router, config } = app

  const rootPath = '/'
  const { apiRouterPath } = config.settings
  router.get(rootPath, controller.home.index)
  const { auth } = controller
  router.post(`${rootPath}login`, auth.sign)
  router.get(`${apiRouterPath}decode`, auth.decode)
  router.get(`${apiRouterPath}verify`, auth.verify)
  router.get('/user/current', auth.currentUser)

  routeEntities(app)
}
